<?php
/**
 * Created by PhpStorm.
 * User: fanzhaogui
 * Date: 2021/6/16
 * Time: 14:55
 */

namespace app\task\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 发送消息
 *
 *  项目根目录下命令行执行 php think SendMessage 可以触发
 *
 * @package app\task\command
 */
class SendMessage extends Command
{
    public function configure()
    {
        $this->setName("SendMessage")->setDescription("计划任务 SendMessage");
    }

    /*
     * 当调用此类时， 会自动调用 execute 方法
     */
    protected function execute(Input $input, Output $output)
    {
        $output->writeln("Data crontab job start");

        $this->birthday();

        // $this->secTask();

        $output->writeln("-------end---------");
    }

    /**
     * 自定义事件
     *
     * @desc https://www.kancloud.cn/thinkphp/think-swoole/722895
     * @desc https://blog.csdn.net/jmkweb/article/details/89684225 毫秒器任务案例
     */
    public function birthday()
    {
        echo "todo something。。。。。。。。。。!\r\n";
    }

    public function secTask()
    {
        $serv = new \swoole_server("0.0.0.0",9505);
        $serv->on("WorkerStart",function ($serv, $woker_id){
            if ($woker_id == 0){
                //每隔1000ms触发一次
                swoole_timer_tick(1000, function ($timer_id) {
                     echo "hello " . date('Y-m-d H:i:s'). "\n";
                });
            }
        });

        $serv->on("receive",function ($serv,$fd,$from_id,$data){
            $serv->send($fd,"Server:".$data);
        });

        $serv->start();
    }
}